package controller;

import model.FileRules;
import view.Rules;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * description: 账户及其教室信息的验证类
 * author: 时间
 * time: 2022-01-10-12:27
 */
public class AccountValidation {
    static OperationFile operationFile = new OperationFile();
//    static Pattern pattern = Pattern.compile("\\((.*?)-");

    /**
     * Description: 检测账号是否存在
     * Author: 时间
     * Date: 2022/1/10 15:10
     */
    public static boolean accountExist(String account, Pattern pattern){
        StringBuffer string = operationFile.readRegisteredFile();
        Matcher matcher = pattern.matcher(string);
        while (matcher.find()) {
            String str = matcher.group(0);  // (201920321243-
            // 字符串截取，去掉第一个与最后一个
            String str1 = str.substring(1, str.length() - 1);   // 201920321243
            if (str1.equals(account))
                return true;
        }
        return false;
    }

    /**
     * Description: 检测账号与密码是否正确
     * Author: 时间
     * Date: 2022/1/10 15:25
     */
    public static boolean passwordExist(String account, String password, Pattern pattern){
        StringBuffer string = operationFile.readRegisteredFile();
        Matcher matcher = pattern.matcher(string);
        while (matcher.find()) {
            String str = matcher.group(0);  // (201920321243-
            // 字符串截取，去掉第一个与最后一个
            String str1 = str.substring(1, str.length() - 1);   // 201920321243
            String[] strArray = str1.split("-");
//            System.out.println(strArray[0] + " " + strArray[1]);
            if (strArray[0].equals(account) && strArray[1].equals(password)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Description: 判断该账号是否存在于学生或教师信息中，参数：账号，正则匹配规则
     * Author: 时间
     * Date: 2022/1/11 20:04
     */
    public static boolean inferAccountExist(String account){
        StringBuffer string1 = operationFile.readStudentFile();
        ToArray toArray = new ToArray();
        String[][] strings1 = toArray.fileToArray(string1, FileRules.pathSt, Rules.pattern2);
        for (int i = 0; i < strings1.length; i++) {
            if (strings1[i] != null){
                if (strings1[i][3].equals(account)){
                    return true;
                }
            }
        }
        StringBuffer string2 = operationFile.readTeacherFile();
        String[][] strings2 = toArray.fileToArray(string2, FileRules.pathTe, Rules.pattern2);
        for (int i = 0; i < strings2.length; i++) {
            if (strings2[i] != null){
                if (strings2[i][3].equals(account)){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Description: 判断教室号是否存， 参数：教室号, 正则匹配规则
     * Author: 时间
     * Date: 2022/1/12 16:06
     */
    public static boolean classNumExist(String count, Pattern pattern){
        StringBuffer string = operationFile.readClassroomFile();
        Matcher matcher = pattern.matcher(string);
        while (matcher.find()) {
            String str = matcher.group(0);  // (201920321243-
            // 字符串截取，去掉第一个与最后一个
            String str1 = str.substring(1, str.length() - 1);   // 201920321243
            if (count.equals(str1)){
                return true;
            }
        }
        return false;
    }

    /**
     * Description: 判断是否有预约
     * Author: 时间
     * Date: 2022/1/15 15:08
     */
    public static boolean orderExist(String count, Pattern pattern){
        StringBuffer stringBuffer = operationFile.checkOrderClassroom();
        Matcher matcher = pattern.matcher(stringBuffer);
        while (matcher.find()) {
            String str = matcher.group(0);  // (201920321243-
            // 字符串截取，去掉第一个与最后一个
            String str1 = str.substring(1, str.length() - 1);   // 201920321243
            if (count.equals(str1)){
                return true;
            }
        }
        return false;
    }

    /**
     * Description: 判断预约教室是否为空闲
     * Author: 时间
     * Date: 2022/1/15 15:10
     */
    public static boolean freeExist(String number, Pattern pattern, StringBuffer stringBuffer) throws IOException {
//        StringBuffer stringBuffer = operationFile.readsmartClassroom();
        Matcher matcher = pattern.matcher(stringBuffer);
        while (matcher.find()){
            String str = matcher.group(0);
            String str1 = str.substring(1, str.length() - 1);
            String[] strings = str1.split("-");
            if (number.equals(strings[0])){
                if (strings[3].equals("空闲")){
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Description: 判断该教室是否已在预约文件中，参数：教室号，正则2，用于取消预约
     * Author: 时间
     * Date: 2022/1/15 22:49
     */
    public static boolean orderClassExist(String number, Pattern pattern){
        StringBuffer stringBuffer = operationFile.checkOrderClassroom();
        Matcher matcher = pattern.matcher(stringBuffer);
        while (matcher.find()) {
            String str = matcher.group(0);  // (201920321243-
            // 字符串截取，去掉第一个与最后一个
            String str1 = str.substring(1, str.length() - 1);   // 201920321243
            String[] strings = str1.split("-");
            if (number.equals(strings[1])){
                return true;
            }
        }
        return false;
    }
}
